package com.azt.front.action.product;

import com.azt.api.dto.shop.ProSearchParam;
import com.azt.api.pojo.Category;
import com.azt.api.service.ProCategoryService;
import com.azt.api.service.ProductService;
import com.azt.model.page.TPagination;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**供求信息
 * 平台产品列表
 * Created by 张栋 on 2017/2/1710:54
 */
@Controller
@RequestMapping("selloffer")
public class ProductSellOfferAction {

    @Autowired
    private ProCategoryService proCategoryService;


    @Autowired
    private ProductService productService;



    /**
     * 获取热门商品
     * Created by 张栋 on 2017/2/20 11:23
     */
    private TPagination getHotProduct() {
        ProSearchParam proSearchParam = new ProSearchParam();
        proSearchParam.setPageSize(6);
        proSearchParam.setOrderType(6);//浏览量desc
        return productService.getProductList(proSearchParam);
    }

    /**
     * 商城首页
     * Created by 张栋 on 2017/2/17 13:26
     */
    @RequestMapping("")
    public String sellOffer(ProSearchParam param,ModelMap model){
        String categoryIds = param.getCategoryIds();
        Integer categoryid =0;
        if(StringUtils.isNotBlank(categoryIds)){
            try {
                categoryid = Integer.parseInt(categoryIds);
            } catch (NumberFormatException e) {
                categoryIds="";
            }
        }


        Category category = proCategoryService.getCacheCategoryById(categoryid, false, false);
        model.put("category",category);
        param.setCategoryIds(categoryIds);
        TPagination hots = getHotProduct();
        model.put("hots",hots.getList());
        model.put("param",param);
        return "product/sell_offer";
    }


    /**
     * 异步检索商品
     * Created by 张栋 on 2017/2/17 13:25
     */
    @RequestMapping("offer_search")
    @ResponseBody
    public TPagination offerSearch(@RequestBody ProSearchParam param){

        Integer catid =0;
        try {
            String categoryIds = param.getCategoryIds();
            catid = Integer.parseInt(categoryIds);
        } catch (NumberFormatException e) {
        }

        param.setPageSize(20);
        //获取检索到的产品
        TPagination products = productService.getProductList(param);
        //查下一级类目信息
        List<Category> childenCategorys = proCategoryService.getChildenCategorys(catid);
        if(CollectionUtil.isEmpty(childenCategorys)){
            //为空时可能没有下级了,这时取当前同级的类目
            Category category = proCategoryService.getCategorysById(catid);
            if(category==null){//同级不存在,数据有问题
                return null;
            }else{
                childenCategorys = proCategoryService.getChildenCategorys(category.getPid());
            }
        }

        products.setParam("categoryList",childenCategorys);
        products.setParam("current",catid);
        Category category = proCategoryService.getCategorysById(catid);
        products.setParam("currentName",catid==0?"全部分类":category.getName());

        if(catid!=0){
            Category ccat = proCategoryService.getCacheCategoryById(catid,true,false);
            products.setParam("ccat",ccat);
        }else{
            products.setParam("ccat",null);
        }

        return products;
    }

}
